<?php

namespace App\Http\Controllers;

use App\Setting;
use App\Writing;
use App\WritingLike;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;

class IndexController extends Controller
{
    //起始页面
    public function index(Request $request){

        return view('web.init');
    }
    //首页
    public function home(Request $request){

        //幻灯片
        //喜欢的作品
        $photography=Writing::with(['user','theme','comment'=>function($query){
            return $query->orderBy('id','desc');
        }])->where('is_display',1)->where('category_id',0)->orderBy('likes','desc')->orderBy('id','desc')->take(2)->get();
        $painting=Writing::with(['user','theme','comment'=>function($query){
            return $query->orderBy('id','desc');
        }])->where('is_display',1)->where('category_id',1)->orderBy('likes','desc')->orderBy('id','desc')->take(2)->get();
        
        return view('web.index',compact('photography','painting'));
    }
    //摄影
    public function photography(){
        $category_id=0;
        $photography=Writing::with(['user','theme','comment'=>function($query){
            return $query->orderBy('id','desc');
        }])->where('is_display',1)->where('category_id',0)->orderBy('likes','desc')->orderBy('id','desc')->take(4)->get();

        return view('web.index',compact('photography','category_id'));
    }
    //绘画
    public function painting(){
        $category_id=1;
        $painting=Writing::with(['user','theme','comment'=>function($query){
            return $query->orderBy('id','desc');
        }])->where('is_display',1)->where('category_id',1)->orderBy('likes','desc')->orderBy('id','desc')->take(4)->get();

        return view('web.index',compact('painting','category_id'));
    }


    //得到作品数据
    /**
     * @param Request $request
     *
     */
    public function get_writing(Request $request){

        $writing_type=$request->category_id;
        $writing=Writing::with(['user','theme','like'=>function($query){
            if(Auth::check()){
                return $query->where('user_id',Auth::id());
            }else{
                return $query->where('user_id',null);
            }
        }]);
        if(isset($writing_type)||$writing_type===0){
            $writing=$writing->where('category_id',$writing_type);
        }
        $writing=$writing->where('is_display',1)
            ->orderBy('id','desc')
            ->paginate(10);
        return view('web.writing.get_writing',compact('writing','config'));
    }
    //更换语言
    public function change(Request $request,$language){

        if($language=='English'){

            session(['lang'=>'en']);
        }else{
            session(['lang'=>'Zh-cn']);
        }

        return redirect()->back();
    }
    //联系我们
    public function contact(){
        $setting=Setting::all()->pluck('value','name');
        return view('web.contact',compact('setting'));
    }
    public function like(Request $request){
        if(Auth::check()){
            $writing_id=$request->writing_id;
            $writing=  Writing::findOrFail($writing_id);
            $like=WritingLike::where('writing_id',$writing_id)->where('user_id',Auth::id())->first();
            if(!$like){
                $like=new WritingLike();
                $like->writing_id=$writing_id;
                $like->user_id=Auth::id();
                $like->save();
                $writing->increment('likes',1);
                return [
                    'status'=>'success',
                    'msg'=>''
                ];
            }
        }else{
            return [
                'status'=>'error',
                'msg'=>trans('web.please_login')
            ];
        }
    }
    public function no_like(Request $request){
        if(Auth::check()){
            $writing_id=$request->writing_id;
            $writing= Writing::findOrFail($writing_id);
            $like=WritingLike::where('writing_id',$writing_id)->where('user_id',Auth::id())->delete();
            $writing->decrement('likes',1);
            return [
                'status'=>'success',
                'msg'=>''
            ];

        }else{
            return [
                'status'=>'error',
                'msg'=>trans('web.please_login')
            ];
        }
    }
    public function redis(){
        Redis::set('haha1',111);
    }
}
